var table;
var $;
var layer;
var form;
layui.use(['table','jquery','layer','form'],function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;

    //下拉框
    getChanName();
    function getChanName() {
        $.ajax({
            url: "/goodsTree/getTree",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var d = eval(data.data);
                //console.log(data.data);
                $("#title").append("<option value=''></option>");
                for(var i = 0; i < d.length-1; i++) {
                    var id = d[i].categroyId;
                    var name = d[i].title;
                    $("#title").append("<option value='" + id + "'>" + name + "</option>");
                }
                //刷新下拉框
                form.render('select');
            }
        })
    }
    var result='';
    select("/Record/selectRecord?result="+result);
    function select(url){
        table.render({
            elem:"#root",
            url:url,
            toolbar:'#toptool',
            defaultToolbar:['print','filter','exports'],
            size:"lg",
            page:true,
            limit:5,
            limits:[5,10,15,20],
            cols:[[
                {type:'numbers',title:'序号',align:'center'},
                {field:'productCode',title:'商品编号',align:'center'},
                {field:'mainImage',title:'商品图片',align:'center',templet:function (data) {
                        return '<img src="'+data.mainImage+'"/>';
                    }},
                {field:'productName',title:'商品名称',align:'center'},
                {field:'currentpriceProduct',title:'价格',align:'center'},
                {field:'title',title:'分类',align:'center'},
                {field:'trueName',title:'上传人员',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function (data) {
                        if(data.result=="1"){
                            return "审核通过";
                        }else if(data.result=="2"){
                            return "未审核";
                        }else{
                            return "审核未通过";
                        }
                    }},
                {title:'操作',align:'center',templet:function (data) {
                        if(data.result=="1"|data.result=="3"){
                            return "<div>" +
                                "<a href='javascript:chakan("+data.productId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "</div>";
                        }else{
                            return "<div>" +
                                "<a href='javascript:chakan("+data.productId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "<a href='javascript:shenhe(\""+data.productId+"\",\""+data.productName+"\");' style='color: #0EAA9E'>审核</a><br/>"+
                                "</div>";
                        }
                    }}
            ]]
        });
    }

    $("#b1").click(function () {
        yes('b1');
        no('b2');
        no('b3');
        no('b4');
        select("/Record/selectRecord?result="+result);
    });

    $("#b2").click(function () {
        no('b1');
        yes('b2');
        no('b3');
        no('b4');
        var result='1';
        select("/Record/selectRecord?result="+result);
    });

    $("#b3").click(function () {
        no('b1');
        no('b2');
        yes('b3');
        no('b4');
        var result='2';
        select("/Record/selectRecord?result="+result);
    });
    $("#b4").click(function () {
        no('b1');
        no('b2');
        no('b3');
        yes('b4');
        var result='3';
        select("/Record/selectRecord?result="+result);
    });

    function no(id) {
        var box = document.getElementById(id);
        box.style.backgroundColor='#F5F7F9';
        box.style.color='#666666';
        box.style.border="1px solid #D2D2D2";
    }
    function yes(id) {
        var box = document.getElementById(id);
        box.style.backgroundColor='#009688';
        box.style.color='white';
        box.style.border="1px solid #D2D2D2";
    }
    $("#sousuo").click(function () {
        var productName = $("#productName").val();
        var title = $("#title").val();
        var trueName = $("#trueName").val();
        select("/Record/selectThree?productName="+productName+"&title="+title+"&trueName="+trueName);
    });

    $("#chongzhi").click(function () {
        $("#productName").val("");
        $("#title").val("");
        $("#trueName").val("");
        var result='';

        select("/Record/selectRecord?result="+result);
    });
    
    $("#shuaxin").click(function () {
        location.reload();
    });

    selectNum($("input[name=quanbu]"),"");
    selectNum($("input[name=tongguo]"),"1");
    selectNum($("input[name=daishen]"),"2");
    selectNum($("input[name=weitongzhi]"),"3");
    function selectNum(ming,result) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"result":result},
            url:"/Record/selectNum",
            success:function (data) {
                ming.val(data.data);
            }
        })
    };

    $('.layui-btn-container').children('button').click(function () {
        $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
    })
    $("#jiansuo").click(function () {
        var obj;
        layer.open({
            type: 2,
            shade: 0,
            title:"高级检索",
            content:"/goods/goods-search.html",
            area: ['600px', '380px'],
            btn:["确定","取消"],
            yes:function(index){

                var productCode = obj.find("#productCode").val();
                var title = obj.find("#title").val();
                var productName = obj.find("#productName").val();
                var currentpriceProduct = obj.find("#currentpriceProduct").val();
                var trueName = obj.find("#trueName").val();
                var result = obj.find("#result").val();
                select(
                    "/Record/selectGaoJi?productCode="+ productCode
                        +"&title="+title
                        +"&productName="+productName
                        +"&currentpriceProduct="+currentpriceProduct
                        +"&trueName="+trueName
                        +"&result="+result
                );
                layer.close(index);
            },btn2:function(index){
                layer.close(index);
            },
            success:function (layero,index) {
               obj = layero.find('iframe').contents();
            }
        })
    });
    //查看商品详情
    chakan = function chakan(productId){
        $.ajax({
            url:"/goodsTable/getProduct",
            type:"post",
            dataType:"json",
            data:{"productId":productId},
            success:function(data){
                devicesMsg(data);
            },
        })
    }

    function devicesMsg(data){
        layer.open({
            type: 2,
            title:"商品审核详情",
            shade: 0,
            maxmin:true,
            content: "/goods/goods-see.html",
            area:["1000px","620px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },
            success:function(layero,index){
                var obj = layero.find('iframe').contents();
                obj.find("#productCode").val(data.data.productCode);
                obj.find("#productName").val(data.data.productName);
                obj.find("#currentpriceProduct").val(data.data.currentpriceProduct+"元");
                obj.find("#title").val(data.data.title);
                if (data.data.productSate == 0) {
                    obj.find("#productSate").val("已删除");
                } else if (data.data.productSate == 1) {
                    obj.find("#productSate").val("上架");
                } else if (data.data.productSate == 2) {
                    obj.find("#productSate").val("下架");
                } else {
                    return "";
                }
                obj.find('#saleNum').val(data.data.saleNum);
                obj.find("#hits").val(data.data.hits);
                obj.find("#collectionNum").val(data.data.collectionNum);
                obj.find("#productDescription").val(data.data.productDescription);
                obj.find("#productLabel").val(data.data.productLabel);
                obj.find("#mainImage").attr('src', data.data.mainImage);
            }
        });
    };

    shenhe = function shenhe(productId,productName) {
        var obj;
        layer.open({
            type:2,
            title:'商品审核',
            content:"/goods/goods-commodity-audit.html",
            maxmin:true,
            area:["450px","340px"],
            btn:["确定","取消"],
            yes:function(index){
                var implementId = localStorage.getItem("createId");
                var implementDate = new Date();
                implementDate = getTime(implementDate);
                var result = localStorage.getItem("result");
                var json =
                    {
                        result:result,
                        remarks:obj.find("#remarks").val(),
                        implementId:implementId,
                        implementDate:implementDate,
                        id:obj.find("#id").val(),
                        type:"3"
                    };
                $.ajax({
                    type:"post",
                    url:"/Record/insertRecord",
                    dataType:"json",
                    data:json,
                    success:function (data) {
                        if(data){
                            layer.msg("审核成功");
                            location.reload();
                        }else{
                            layer.msg("审核失败");
                        }
                    }
                });
            },btn2:function(index){
                layer.close(index);
            },
            success:function (layero,index) {
               obj = layero.find('iframe').contents();
               obj.find("#shopName").text(productName);
               obj.find("#id").val(productId);
            }
        });
    }
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    }
});

//跳转
function goBack() {
    //通过页面查找iframe
    parent.$('#recordLog').val($('#examine').text());
    parent.before();
    return false;
}
